home *** CD-ROM | disk | FTP | other *** search
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 6
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IThe Fundamentals Of Structured Design~Y~I
-
- Structured design is very easy. It's based on just a few simple ideas. The
- first is that a ~R~Istructured program~Y~I is a ~C~Iproper program ~Y~Icomposed of ~C~Iproper
- program segments~Y~I. ~C~IProper program segments ~Y~Iare built with a combination of
- ~M~Iselection~Y~I, ~M~Iiteration~Y~I, ~M~Isequence~Y~I, and ~M~Irecursion~Y~I. They use as much information
- and |implementation hiding| as possible. A proper program segment has the
- following characteristics:
-
- 1. Every module has a single entry and a single exit.
- 2. There is no "dead code".
- 3. There are no endless loops.
-
- #WN
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 7
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~ISingle Entry, Single Exit~Y~I
-
- Single entry, single exit means just that; there is only one way to get in
- or out of a program segment. This necessarily implies that there are no
- GOTO's or other funky jumps to or from who-knows-where. Have you ever seen
- a jump from the Twilight Zone into the middle of an if-then-else statement?
- I have. Believe me it's no fun to maintain. Especially when there are
- several of them chained together.
-
- #WN
- Multiple entries into a section of code and multiple exits out of it
- increase the number of paths through the code section |exponentially|. In
- other words, adding a few jumps into or out of a section of code makes it
- much harder to test fully. Code that is not fully tested is by definition
- more likely to have bugs.
-
- #WN
- #C4,R20
- In addition, the |dependencies| between sections of code grows when we do
- unstructured jumps. This decreases the generality of the code, and
- therefore decreases our ability to plug the code into another program. That
- means we're probably not going to be very successful at being lazy.
-
- #WP
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 8
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IDead Code~Y~I
-
- ~M~IDead code~Y~I is code that can't ever be executed. It usually appears after
- there's been a modification. Something gets changed or taken out, and a
- statement is now in a location where it's no longer executed. Figure 5.2
- shows an example of dead code.
-
- ~W~IFigure 5.2~Y~I
- Dead Code
- .
- .
- .
- /* Lots of code preceding the dead code */
- goto somewhere_else;
- ~R~Ii++; ~Y~I/* This is all dead code. */~R~I
- trezzenvop=sqrt(gribble); ~Y~I/* It can't be executed */~R~I
- printf("Zerbleflatz\n"); ~Y~I/* unless the goto is */~R~I
- snert=getchar(); ~Y~I /* deleted. */
-
- #WN
- #BO,44,8,78,22,7,1,0,13,15,6
- This is a time bomb waiting to
- explode. After a while, no one
- knows what these statements are
- for so they're afraid to take
- them out. One day when you're
- least expecting it, the code
- will change again and those
- statements will once more be
- executed. And like Dracula
- raised from the dead, they
- will invariably bite you in a
- big way and suck up all your
- time.
-
- #WP
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 9
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IEndless Loops~Y~I
-
- Endless loops are unfortunately all too common in C programs. To be totally
- honest, I use them once in a great while myself. But be warned, if they're
- not used sparingly they'll completely undermine the reliability of your
- program. Figure 5.3 shows an endless loop.
-
-
- ~W~IFigure 5.3~Y~I
- An endless loop
-
- /* This is endless because there is no specified termination
- condition. */
- for ~W~I(;;)~Y~I
- {
- do_a_function(a_parameter, another_parameter);
- a_parameter *= another_parameter - strlen(something_profound);
- }
-
- #WP
- %
- #EF
- #T15,1,Chapter 5 General Design Considerations Pg. 10
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~IIn my work on a certain software project, I found that one of the Software
- Engineers on the program before me used endless loops quite liberally. The
- result was that, in many cases, the software would lock up after I made a
- slight modification. It was infuriating to have to try and track down the
- endless loop responsible. This usually wasn't easy because the computer I
- was working on was a |parallel processing system|, and the loops often
- involved interaction between the processors.
-
- #WN
- #C4,R13
- The only time I use endless loops is in applications that ~S~M~Inever terminate~Y~I,~s
- like operating systems. Even then, I only put them in at the ~W~Ihighest level~Y~I
- possible. Moderation is the best policy when it comes to endless loops.
- #WP
- #X